package com.dmooo.yxmmerchant.fragment.model;

import android.content.Context;

import com.common.net.entity.SubjectPost;
import com.common.net.listener.HttpOnNextListener;
import com.common.net.mywidget.LoadingDialog;
import com.common.net.subscribers.ProgressSubscriber;
import com.common.util.SPUtils;
import com.dmooo.yxmmerchant.common.Config;
import com.dmooo.yxmmerchant.fragment.contract.ServiceOrderContract;
import com.dmooo.yxmmerchant.http.HttpManager;
import com.dmooo.yxmmerchant.view.ui.UserInfoActivity;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ServiceOrderModel implements ServiceOrderContract.ServiceOrderMdl {
    private Context context;
    private LoadingDialog loadingDialog;
    public ServiceOrderModel(Context context) {
        this.context=context;
        loadingDialog=new LoadingDialog(context);
        loadingDialog.setTitle("发布中");
    }


    @Override
    public void getUserOrderList(String token, int status, int page, HttpOnNextListener listener) {
        Map<String,Object> map=new HashMap<>();
        map.put("token",token);
        map.put("status",status);
        map.put("p",page);
        map.put("per",10);
        if(context instanceof UserInfoActivity){
            map.put("user_id",Config.uid);
        }else{
            if("0".equals(SPUtils.get(context,"search_index","").toString())){
                map.put("service_name",SPUtils.get(context,"search_content","").toString());
            } else if("1".equals(SPUtils.get(context,"search_index","").toString())){
                map.put("user_account",SPUtils.get(context,"search_content","").toString());
            }else{
                map.put("order_num",SPUtils.get(context,"search_content","").toString());
            }
        }
        SubjectPost post=new SubjectPost(new ProgressSubscriber(listener,context,false,false,"删除中"),map);
        HttpManager.getInstance().getServiceOrderList(post);
    }

    @Override
    public void cancleOrder(String token, String order_id, HttpOnNextListener listener) {
//        Map<String,Object> map=new HashMap<>();
//        map.put("token",token);
//        map.put("order_id",order_id);
//        SubjectPost post=new SubjectPost(new ProgressSubscriber(listener,context,true,false,"删除中"),map);
//        HttpManager.getInstance().cancleServiceOrder(post);
    }

    @Override
    public void getLogisticsList(HttpOnNextListener listener) {
        Map<String,Object> map=new HashMap<>();
        SubjectPost post=new SubjectPost(new ProgressSubscriber(listener,context,true,true,"删除中"),map);
        HttpManager.getInstance().getLogisticsList(post);
    }

    @Override
    public void confirmOrder(String token, String order_detail_id, HttpOnNextListener listener) {
        Map<String,Object> map=new HashMap<>();
        map.put("token",token);
        map.put("order_detail_id",order_detail_id);
        SubjectPost post=new SubjectPost(new ProgressSubscriber(listener,context,true,false,"删除中"),map);
        HttpManager.getInstance().confirmServicePay(post);
    }

    @Override
    public void getLogisticsDetail(String token,String logistics, String express_number, HttpOnNextListener listener) {
        Map<String,Object> map=new HashMap<>();
        map.put("token",token);
        map.put("logistics",logistics);
        map.put("express_number",express_number);
        SubjectPost post=new SubjectPost(new ProgressSubscriber(listener,context,false,false,"删除中"),map);
        HttpManager.getInstance().getLogisticsMsg(post);
    }

    @Override
    public void fillInRefundExpressNum(String token, String order_detail_id, HttpOnNextListener listener) {
        Map<String,Object> map=new HashMap<>();
        map.put("token",token);
        map.put("order_detail_id",order_detail_id);
        SubjectPost post=new SubjectPost(new ProgressSubscriber(listener,context,true,false,"删除中"),map);
        HttpManager.getInstance().finishService(post);
    }


    @Override
    public void checkServiceBackMoney(String token, String order_detail_id, String check_result, String drawback_refuse_reason, HttpOnNextListener listener) {
        Map<String,Object> map=new HashMap<>();
        map.put("token",token);
        map.put("order_detail_id",order_detail_id);
        map.put("check_result",check_result);
        map.put("drawback_refuse_reason",drawback_refuse_reason);
        SubjectPost post=new SubjectPost(new ProgressSubscriber(listener,context,true,false,"删除中"),map);
        HttpManager.getInstance().serviceCheckRefund(post);
    }

    @Override
    public void getOrderDetail(String order_id, String order_detail_id, HttpOnNextListener listener) {
        Map<String,Object> map=new HashMap<>();
        map.put("order_id",order_id);
        map.put("order_detail_id",order_detail_id);
        SubjectPost post=new SubjectPost(new ProgressSubscriber(listener,context,true,false,"删除中"),map);
        HttpManager.getInstance().getServiceOrderDetail(post);
    }

    @Override
    public void applyBackMoney(String token, String order_detail_id, String drawback_reason, List<File> drawback_img,final HttpOnNextListener listener) {
//        try {
//            RequestParams requestParams = new RequestParams();
//            for (int i = 0; i < drawback_img.size(); i++) {
//                requestParams.put("drawback_img[" + i + "]", drawback_img.get(i));
//            }
//            AsyncHttpClient client1 = new AsyncHttpClient();
//            client1.addHeader("token", token);
//            requestParams.put("token", token);
//            requestParams.put("order_detail_id", order_detail_id);
//            requestParams.put("drawback_reason", drawback_reason);
//            client1.post(Config.APP_INTERFACE_BASE_URL + "/merchant.php?c=ServiceOrder&a=applyDrawback", requestParams, new TextHttpResponseHandler() {
//
//                @Override
//                public void onStart() {
//                    super.onStart();
//                    loadingDialog.show();
//                }
//
//                @Override
//                public void onFinish() {
//                    super.onFinish();
//                    loadingDialog.dismiss();
//                }
//
//                @Override
//                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
//                    listener.onFail(responseString);
//                }
//
//                @Override
//                public void onSuccess(int statusCode, Header[] headers, String responseString) {
//                    LogUtil.e("backdata", responseString);
//                    try {
//                        JSONObject object1 = new JSONObject(responseString);
//                        if (object1.getInt("code") == 0) {
//                            listener.onNext("");
//                        } else {
//                            listener.onFail(object1.getString("msg"));
//                        }
//                    } catch (JSONException e) {
//                        e.printStackTrace();
//                    }
//
//                }
//            });
//        } catch (FileNotFoundException e) {
//            LogUtil.e("backdata", e.toString());
//        }
    }

}
